package com.htt.app.amnhac12h.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.htt.app.amnhac12h.AmNhac12h;
import com.htt.app.amnhac12h.R;
import com.htt.app.amnhac12h.object.Show;
import com.htt.app.amnhac12h.sqlite.DAOShow;
import com.htt.app.amnhac12h.store.ImageLoader;

public class ListFavoriteAdapter extends ArrayAdapter<Show>{
	
	private Activity activity;
	private int resource;
	private static LayoutInflater inflater = null;
	public static List<Show> data = new ArrayList<Show>();
	private ArrayAdapter<Show> adapter = null;
	private ImageLoader imageLoader;
	
	public ListFavoriteAdapter(Activity activity, int textViewResourceId,
			List<Show> objects) {
		super(activity, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		this.activity = activity;
		this.resource = textViewResourceId;
		ListFavoriteAdapter.data = objects;
		adapter = this;
		imageLoader = new ImageLoader(activity);
	}
	
	public static class ViewHolder {
		ImageView image;
		TextView name;
		TextView des;
		ImageView star;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		final ViewHolder holder;
		if(convertView == null) {
			v = inflater.inflate(resource, null);
			holder = new ViewHolder();
			holder.image = (ImageView) v.findViewById(R.id.list_channel_item_img);
			holder.name = (TextView) v.findViewById(R.id.list_channel_item_name);
			holder.des = (TextView) v.findViewById(R.id.list_channel_item_des);
			holder.star = (ImageView) v.findViewById(R.id.list_channel_item_star);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		final Show show = getItem(position);
		holder.name.setText(show.getName());
		holder.des.setText(show.getDesc());
		
		/*****************************************************/
		
		DAOShow db = new DAOShow(activity);
		db.open();
		if(db.checkShow(show)) {
			holder.star.setImageResource(R.drawable.icon_star_pressed);
		} else
			holder.star.setImageResource(R.drawable.icon_star);
		db.close();
		
		/******************************************************/
		
		if(show.getIcon()!=null && !show.getIcon().trim().equals("") ) {
			imageLoader.DisplayImage(show.getIcon(), holder.image);
		} else {
			holder.image.setImageResource(com.htt.app.amnhac12h.utils.Utils.getImageRadom());
		}
		
		/*******************************************************/
		
		// hander event click item list and star
				holder.star.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						DAOShow db = new DAOShow(activity);
						db.open();
						
						if(db.checkShow(show)) {
							db.deleteShow(show);
							holder.star.setImageResource(R.drawable.icon_star);
						}
						else {
							db.insertShow(show);
							holder.star.setImageResource(R.drawable.icon_star_pressed);
						}
						
						db.close();
					}
				});
		
		// hander event click item list and star
		v.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AmNhac12h.home_view.removeAllViews();
				if(AmNhac12h.playerView.isCreate) {
					AmNhac12h.home_view.addView(AmNhac12h.playerView.getView());
					AmNhac12h.playerView.play(show);
					AmNhac12h.changeStatusViewDisplay(AmNhac12h.VIEWPLAYER);
				}
			}
		});
		
		return v;
	}
	
}


